//
//  NSView+FillWithView.m
//  WeatherApp
//
//  Created by Julia on 7/25/14.
//  Copyright (c) 2014 Yuliya Grasevych. All rights reserved.
//

#import "NSView+FillWithView.h"

@implementation NSView (FillWithView)

-(void)fillWithView:(NSView *)childView
{
    if (childView) {
        // add subview and layout it to fill whole view
        [self addSubview:childView];
        
        [childView setTranslatesAutoresizingMaskIntoConstraints:NO];
        
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[childView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(childView)]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[childView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(childView)]];
    }
}

-(void)replaceSubview:(NSView *)oldView withFill:(NSView *)newView
{
    if (newView) {
        if (oldView) {
            // replace subview and layout it to fill whole view
            [self replaceSubview:oldView with:newView];
        }
        else {
            [self addSubview:newView];
        }
        [newView setTranslatesAutoresizingMaskIntoConstraints:NO];
        
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[newView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(newView)]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[newView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(newView)]];
        
    }
}
@end
